package dao;

import java.sql.CallableStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;
import java.util.logging.Level;
import java.util.logging.Logger;
import model.Role;
import util.DataUtil;
import model.Staff;

public class StaffDAO {

    public static List<Staff> getAll() {
        List<Staff> list = new ArrayList<Staff>();
        String call = "{Call GET_ALL_STAFF}";
        try {
            CallableStatement cs = DataUtil.getConnection().prepareCall(call);
            ResultSet rs = cs.executeQuery();
            while (rs.next()) {
                Staff model = new Staff();
                model = new Staff();
                model.setStaffId(rs.getString("StaffID"));
                model.setUserName(rs.getString("Username"));
                model.setPassWord(rs.getString("Password"));
                model.setFullName(rs.getString("FullName"));
                model.setBirthDay(rs.getString("Birthday"));
                model.setAddRess(rs.getString("Address"));
                model.setPhone(rs.getString("Phone"));
                model.setGenDer(rs.getBoolean("Gender"));
                model.setEmail(rs.getString("Email"));
                int roleId = rs.getInt("RoleID");
                String roleName = rs.getString("RoleName");
                model.setRl(new Role(roleId, roleName));
                model.setAddRess(rs.getString("Address"));
                list.add(model);
            }
        } catch (SQLException ex) {
            ex.printStackTrace();
        }
        return list;
    }

    public static Staff checkStaff(Staff m) {
        Staff model = null;
        try {
            String call = "{Call GET_STAFF(?,?,?)}";
            CallableStatement cs = DataUtil.getConnection().prepareCall(call);
            cs.setString(1, m.getUserName());
            cs.setString(2, m.getPassWord());
            cs.setInt(3, m.getRl().getId());
            ResultSet rs = cs.executeQuery();
            while (rs.next()) {
                model = new Staff();
                model.setStaffId(rs.getString("StaffID"));
                model.setUserName(rs.getString("Username"));
                model.setPassWord(rs.getString("Password"));
                model.setFullName(rs.getString("FullName"));
                model.setBirthDay(rs.getString("Birthday"));
                model.setAddRess(rs.getString("Address"));
                model.setPhone(rs.getString("Phone"));
                model.setGenDer(rs.getBoolean("Gender"));
                model.setEmail(rs.getString("Email"));
                int roleId = rs.getInt("RoleID");
                String roleName = rs.getString("RoleName");
                model.setRl(new Role(roleId, roleName));
            }
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return model;
    }

    public static boolean checkIDstaff(String staffID) {
        boolean fl = true;
        String call = "{Call CHECK_ID_STAFF(?)}";
        try {
            CallableStatement cs = DataUtil.getConnection().prepareCall(call);
            cs.setString(1, staffID);
            ResultSet rs = cs.executeQuery();
            if (rs.next()) {
                fl = false;
            }
        } catch (SQLException ex) {
            ex.printStackTrace();
        }
        return fl;
    }

    public static boolean checkUsernameStaff(String username) {
        boolean fl = true;
        String call = "{Call CHECK_USERNAME_STAFF(?)}";
        try {
            CallableStatement cs = DataUtil.getConnection().prepareCall(call);
            cs.setString(1, username);
            ResultSet rs = cs.executeQuery();
            if (rs.next()) {
                fl = false;
            }
        } catch (SQLException ex) {
            ex.printStackTrace();
        }
        return fl;
    }

    public static boolean changePassword(String staffID, String password) {
        int rs = 0;
        String call = "{Call CHANGE_PASSWORD(?,?)}";
        try {
            CallableStatement cs = DataUtil.getConnection().prepareCall(call);
            cs.setString(1, staffID);
            cs.setString(2, password);
            rs = cs.executeUpdate();
        } catch (SQLException ex) {
            ex.printStackTrace();
        }
        return (rs > 0) ? true : false;
    }

    public static boolean addStaff(Staff st) {
        int rs = 0;
        String call = "{Call ADD_STAFF(?,?,?,?,?,?,?,?,?,?)}";
        try {
            CallableStatement cs = DataUtil.getConnection().prepareCall(call);
            cs.setString(1, st.getStaffId());
            cs.setString(2, st.getUserName());
            cs.setString(3, st.getPassWord());
            cs.setString(4, st.getFullName());
            cs.setString(5, st.getBirthDay());
            cs.setBoolean(6, st.isGenDer());
            cs.setString(7, st.getEmail());
            cs.setString(8, st.getPhone());
            cs.setString(9, st.getAddRess());
            cs.setInt(10, st.getRl().getId());
            rs = cs.executeUpdate();
        } catch (SQLException ex) {
            ex.printStackTrace();
        }
        return (rs > 0) ? true : false;
    }

    public static boolean editStaff(Staff st) {
        int rs = 0;
        String call = "{Call EDIT_STAFF(?,?,?,?,?,?,?,?,?,?)}";
        try {
            CallableStatement cs = DataUtil.getConnection().prepareCall(call);
            cs.setString(1, st.getStaffId());
            cs.setString(2, st.getUserName());
            cs.setString(3, st.getPassWord());
            cs.setString(4, st.getFullName());
            cs.setString(5, st.getBirthDay());
            cs.setBoolean(6, st.isGenDer());
            cs.setString(7, st.getEmail());
            cs.setString(8, st.getPhone());
            cs.setString(9, st.getAddRess());
            cs.setInt(10, st.getRl().getId());
            rs = cs.executeUpdate();
        } catch (SQLException ex) {
            ex.printStackTrace();
        }
        return (rs > 0) ? true : false;
    }

    public static boolean deleteStaff(String staffID) {
        int rs = 0;
        String call = "{Call DELETE_STAFF(?)}";
        try {
            CallableStatement cs = DataUtil.getConnection().prepareCall(call);
            cs.setString(1, staffID);
            rs = cs.executeUpdate();
        } catch (SQLException ex) {
            ex.printStackTrace();
        }
        return (rs > 0) ? true : false;
    }
}
